﻿Imports System
Imports System.Collections.Generic
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Media.Imaging
Imports System.Windows.Navigation
Imports System.Windows.Shapes
Imports Infragistics.Controls.Charts
Imports PTIC_Util

Namespace Views
    Partial Public Class ViewScenarioCentral

        Private Shared CurrentGaugeModel As XElement

        Dim TickStyle As Windows.Style
        Dim LabelStyle As Windows.Style

        Shared Sub New()
            Dim cgmf = Application.GetResourceStream( _
               New Uri("/OMANE.Modules.TauxGarantis;component/defaultgaugemodel.gdf", System.UriKind.Relative))

            CurrentGaugeModel = XElement.Load(cgmf.Stream)
        End Sub

        Public Sub New()
            MyBase.New()

            Me.InitializeComponent()

            TickStyle = gaugeContainer.Resources("TickStyle")
            LabelStyle = gaugeContainer.Resources("LabelStyle")
            ' Insert code required on object creation below this point.

            ApplyGaugeModel()
        End Sub

        Private DoubleClickProcess = True

        Private Sub gauge_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles gauge.MouseDoubleClick
            Dim echelle = 1.8 / 100

           

            Dim cd = Function(s As String) Double.Parse(s, Globalization.CultureInfo.InvariantCulture)
            Dim cc = Function(s As String) ColorConverter.ConvertFromString(s)


            Dim xtorange = Function(x As XElement)
                               Dim s = New RadialGaugeRange()
                               s.StartValue = cd(x.@StartValue) * echelle
                               s.EndValue = cd(x.@EndValue) * echelle
                               s.InnerExtentStart = cd(x.@InnerExtentStart)
                               s.InnerExtentEnd = cd(x.@InnerExtentEnd)
                               s.OuterExtent = cd(x.@OuterExtent)
                               s.Fill = New SolidColorBrush(cc(x.@BackColor))
                               s.Stroke = New SolidColorBrush(cc(x.@BorderColor))

                               Return s
                           End Function

            Dim xtotick = Function(x As XElement)
                              Dim t = New RadialGaugeTickMarkGroup() With {.TickMarkStyle = TickStyle}
                              t.StartExtent = cd(x.@StartExtent)
                              t.EndExtent = cd(x.@EndExtent)
                              t.Interval = cd(x.@Interval) * echelle
                              t.PostInitial = cd(x.@PostInitial) * echelle
                              t.PreTerminal = cd(x.@PreTerminal) * echelle
                              t.Stroke = New SolidColorBrush(cc(x.@Stroke))
                              RadialGaugeTickMarkGroupExtension.SetSize(t, cd(x.@Size))

                              Return t
                          End Function

            Dim xtolabel = Function(x As XElement)
                               Dim l = New RadialGaugeLabelGroup() With {.LabelStyle = LabelStyle}
                               l.Extent = cd(x.@Extent)
                               l.Interval = cd(x.@Interval) * echelle
                               l.PostInitial = cd(x.@PostInitial) * echelle
                               l.PreTerminal = cd(x.@PreTerminal) * echelle
                               l.Orientation = [Enum].Parse(GetType(RadialLabelOrientation), x.@Orientation)
                               l.FontSize = cd(x.@FontSize)
                               l.Foreground = New SolidColorBrush(cc(x.@FontColor))

                               Return l
                           End Function


            Dim xtoscale = Function(x As XElement)
                               Dim s = New RadialGaugeScale()
                               s.StartValue = cd(x.@StartValue) * echelle
                               s.StartAngle = cd(x.@StartAngle)
                               s.EndValue = cd(x.@EndValue) * echelle
                               s.EndAngle = cd(x.@EndAngle)

                               Dim rs = (From elt In x.<Ranges>.<Range> Select xtorange(elt)).ToArray()
                               For Each elt In rs
                                   s.Ranges.Add(elt)
                               Next

                               Dim ts = (From elt In x.<TickMarks>.<TickMark> Select xtotick(elt)).ToArray()
                               For Each elt In ts
                                   s.TickMarkGroups.Add(elt)
                               Next

                               Dim ls = (From elt In x.<Labels>.<Label> Select xtolabel(elt)).ToArray()
                               For Each elt In ls
                                   s.LabelGroups.Add(elt)
                               Next

                               Return s
                           End Function

            Dim data As XElement

            If DoubleClickProcess Then
                If Keyboard.Modifiers = ModifierKeys.Control Then
                    Dim dlg = New Microsoft.Win32.OpenFileDialog()
                    dlg.DefaultExt = "gdf"
                    dlg.Filter = "Fichiers du créateur de Jauge (*.gdf)|*.gdf"
                    Dim result = dlg.ShowDialog()
                    If Not result Then Return
                    Dim f = dlg.FileName

                    data = XElement.Load(f)
                    CurrentGaugeModel = data
                Else
                    data = Nothing
                End If
            Else
                data = CurrentGaugeModel
            End If

            If data Is Nothing Then Return

            CType(CType(gauge.RenderTransform, TransformGroup).Children(2), RotateTransform).Angle = cd(data.@Rotation)
            gauge.Background = New SolidColorBrush(cc(data.@BackColor))
            gauge.BorderBrush = New SolidColorBrush(cc(data.@BorderColor))

            Dim ss = (From elt In data.<Scales>.<Scale> Select xtoscale(elt)).ToArray()
            gauge.Scales.Clear()
            For Each elt In ss
                gauge.Scales.Add(elt)
            Next

            Dim b = New Binding("ResultatsScenarioCentral.SP") With {.Mode = BindingMode.OneWay}

            Dim ndl = New RadialGaugeNeedle() With { _
                .EndExtent = 1} ', .Response = New GaugeMarkerResponse() With {.Enabled = True}}
            '    .Response = _
            '       'New GaugeMarkerResponse() With { _
            ''    .Enabled = True, _
            ''    .ResponseTime = TimeSpan.FromSeconds(10.0)
            ''} _
            '}

            BindingOperations.SetBinding(ndl, RadialGaugeNeedle.ValueProperty, b)


            gauge.Scales(0).Needles.Add(ndl)

            data = Nothing
            DoubleClickProcess = True
        End Sub

        Private Sub ApplyGaugeModel()
            DoubleClickProcess = False
            gauge_MouseDoubleClick(Nothing, Nothing)
            DoubleClickProcess = True
        End Sub

        Private Sub DataGrid_AutoGeneratingColumn(ByVal sender As Object, ByVal e As System.Windows.Controls.DataGridAutoGeneratingColumnEventArgs)
            'TODO : ajoutez ici l’implémentation du gestionnaire d’événements.
            If (e.PropertyName.Equals(nomTx_CommiContA)) Then
                CType(e.Column, DataGridTextColumn).Binding.StringFormat = "P2"
                CType(e.Column, DataGridTextColumn).Header = descTx_CommiContA
            End If
        End Sub

    End Class




    Public Class MyRadialGaugeRange
        Inherits RadialGaugeRange



        Public Property Info As String
            Get
                Return GetValue(InfoProperty)
            End Get

            Set(ByVal value As String)
                SetValue(InfoProperty, value)
            End Set
        End Property

        Public Shared ReadOnly InfoProperty As DependencyProperty = _
                               DependencyProperty.Register("Info", _
                               GetType(String), GetType(MyRadialGaugeRange), _
                               New FrameworkPropertyMetadata(Nothing))



    End Class


    Public Class MySizeConverter
        Implements IMultiValueConverter

        Public Function Convert(ByVal values() As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IMultiValueConverter.Convert
            If values(0) <= 0D Or values(1) <= 0D Then Return 0D
            Return Math.Max(values(0), values(1))
        End Function

        Public Function ConvertBack(ByVal value As Object, ByVal targetTypes() As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object() Implements System.Windows.Data.IMultiValueConverter.ConvertBack
            Throw New NotImplementedException()
        End Function
    End Class



End Namespace


Public Class RadialGaugeTickMarkGroupExtension
    Inherits DependencyObject



    Public Shared Function GetSize(ByVal element As DependencyObject) As Double
        If element Is Nothing Then
            Throw New ArgumentNullException("element")
        End If

        Return element.GetValue(SizeProperty)
    End Function
    Public Shared Sub SetSize(ByVal element As DependencyObject, ByVal value As Double)
        If element Is Nothing Then
            Throw New ArgumentNullException("element")
        End If

        element.SetValue(SizeProperty, value)
    End Sub

    Public Shared ReadOnly SizeProperty As  _
                           DependencyProperty = DependencyProperty.RegisterAttached("Size", _
                           GetType(Double), GetType(RadialGaugeTickMarkGroupExtension), _
                           New FrameworkPropertyMetadata(1.0))



End Class

Public Class DoubleThicknessConverter
    Implements IValueConverter

    Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
        Return New Thickness(If(value Is Nothing, 0.0, CDbl(value)))
    End Function

    Public Function ConvertBack(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
        Throw New NotImplementedException()
    End Function
End Class

Public Class PercentageConverter
    Implements IValueConverter

    Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
        Return Double.Parse(value).ToString("P0")
    End Function

    Public Function ConvertBack(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
        Throw New NotImplementedException()
    End Function
End Class